package com.jl.card.filesystem;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.annotations.JSConstructor;
import org.mozilla.javascript.annotations.JSFunction;

import com.jl.card.Card;
import com.jl.card.ICard;
import com.jl.card.CardJSProxy;
import com.jl.card.SWException;
import com.jl.js.core.ByteArray;

public class JSFileSystemProxy extends ScriptableObject {

	private Card card;
	private IFileSystem sys;

	public JSFileSystemProxy() {
	}

	@JSConstructor
	public JSFileSystemProxy(Object obj) {
		Object jsCard = Context.jsToJava(obj, ICard.class);
		if (jsCard instanceof Card) {
			this.card = (Card) jsCard;
		} else if (jsCard instanceof CardJSProxy) {
			this.card = ((CardJSProxy) jsCard).getInnerCard();
		}else{
			throw new RuntimeException("parameter one must be instance of Card");
		}
		this.sys = this.card.getProvider().getFileSystem(card);
	}

	@JSFunction
	public void create(String parent, ByteArray data) throws SWException {
		sys.create(parent, data.getBytes());
	}

	@JSFunction("deleteFile")
	public void deleteFile(String filePath) throws SWException {
		sys.delete(filePath);
	}

	@Override
	public String getClassName() {
		return "FileSystem";
	}

}
